<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {

	public function _initialize(){
                        $this->loadexample();
                        $this->loadGoodsul();
             }
             public function isLogin(){
		if (!isset($_SESSION['USERID']) || $_SESSION['USERID']=='' ) {
			$this->redirect("/Home/User/index",'',1,'你还没有登录，请先登录后再进行操作！');
		}else{
			return true;
		}
	}
	//载入头头部的展示页面
	public function loadexample(){
		$TwoSort = M("Twosort");
		$exampletwosort = $TwoSort->order("id desc")->limit(4)->select();
		$this->assign("exampletwosort",$exampletwosort);
		$Goods = M("Goods");
		$examplegoods =$Goods->order("id desc")->limit(4)->select();
		$this->assign("examplegoods",$examplegoods);
	}
	//载入展示所有头部商品分类
	public function loadGoodsul(){
		$Sort = M("Sort");
		$Sortdata = $Sort->order("id ASC")->select();
		if (empty($Sortdata)) {
			//$this->response("载入商品一级分类信息",false,0,"商品一级分类信息为空");
		}else{
			$TwoSort = M("Twosort");
			$Goods = M("Goods");
			$sortlist = array();
			foreach ($Sortdata as  $val) {
				//商品一级分类图标解码
				$val['sortimgurl']  = json_decode($val['sortimgurl'],true);
				//查找商品二级分类信息
				$Twosortdata = $TwoSort->where("sortid = {$val['id']}")->select();
				
				$val['twosortdata'] = $Twosortdata;
				foreach ($Twosortdata as $key=>$value){
					//商品二级分类图标解码
					$val['twosortdata'][$key]['twosortimgurl'] = json_decode($value['twosortimgurl'],true);
					//查找对应二级分类下的商品信息
					$Goodsdata = $Goods->where("twosortid = {$value['id']} ")->limit(3)->select();
					$val['twosortdata'][$key]['goodsdata'] = $Goodsdata;
				}
				$sortlist[ ] = $val;
			}
			//$this->response("载入商品信息ul",true,1,$sortlist);
			$this->assign("sortlist",$sortlist);
		}
	}
	
	//获取用户购物车信息
	public function getshopcart(){
		$userid = $_SESSION['USERID'];
		$cartlist = M("Cartlist");
		$res = $cartlist->where("userid={$userid}")->find();
		if ($res) {
			$cartgoodslist = M("Cartgoodslist");
			$Goods = M("Goods");
			//如果是结算页面则选出
			if(ACTION_NAME==pay){
				$shopcartinf=$cartgoodslist->where("userid ={$userid} AND isselect=1 ")->select();
			}else{
				$shopcartinf=$cartgoodslist->where("userid ={$userid}")->select();
			}
			
			// $shopcartnum = $cartgoodslist->where("userid ={$userid}")->count();
			$shopcartlist = array();
			$sumprices = 0;
			$shopcartnum =0;
			foreach ($shopcartinf as $value) {
				$onegoods=$Goods->where("id={$value['goodsid']}")->find();
				$value['goodsname'] = $onegoods['goodsname'];
				$value['goodsorig'] = $onegoods['goodsorig'];
				$value['goodsnow'] = $onegoods['goodsnow'];
				$onegoods['goodsimg'] = json_decode($onegoods['goodsimg'],true); 
				$value['goodsimg'] = $onegoods['goodsimg'][0];
				$shopcartlist[] =$value;
				//计算商品总价 与 总数量
				$sumprices = $value['goodsnow']*$value['number']+$sumprices;
				$shopcartnum =$value['number'] +$shopcartnum;
			}
			$this->assign("shopcartnum",$shopcartnum);
			$this->assign("shopcartsumprices",$sumprices);
			$this->assign("shopcartgoodslist",$shopcartlist);
		}
	}
	//获取用户收藏
	public function getcolmsg(){
		$Collection = M("Collection");
		$userid = $_SESSION['USERID'];
		$colsum=$Collection->where("userid = {$userid}")->count();
		$this->assign("colsum",$colsum);
	}
	//获取用户收货地址
	public function getAddress(){
		//获取省份
		$Province = M("Province");
		$provincelist=$Province->order("id asc")->select();
		//获取用户地址
		$user_id = $_SESSION['USERID'];
		$Address = M('Useraddress');
		$Addressdata = $Address->where("userid ={$user_id}")->order('isdefault desc')->select();
		if (!empty($Addressdata)) {
			$City = M("City");
			$District = M('District');
			$useraddlist = array();
			foreach ($Addressdata as $value) {
				$value['province'] = $Province->where("id ={$value['province']}")->getField("provincename");
				$value['city'] = $City->where("id = {$value['city']}")->getField("cityname");
				$value['district'] = $District->where("id ={$value['district']}")->getField("districtname");
				$useraddlist[ ] =$value;
			}
			$this->assign("useraddresslist",$useraddlist);
		}
		$this->assign("provincelist",$provincelist);
	}

}